package ge.unihack.instantdj.protocol.common;

import java.io.BufferedWriter;
import java.io.IOException;
import java.io.OutputStream;
import java.io.OutputStreamWriter;

import org.json.JSONException;
import org.json.JSONObject;

public class JSONCommandWriter implements CommandWriter {

	private static final String LINE_SEPARATOR = System.getProperty("line.separator", "\n");
	
	@Override
	public void writeCommand(OutputStream out, Command command) throws IOException {
		JSONObject jsonCommand = new JSONObject();
		try {
			jsonCommand.put("type", command.getType().name());
		} catch (JSONException e) {
			throw new IOException("Error while writing JSONCommand: " + e.toString());
		}
		String json = jsonCommand.toString();
		BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(out));
		bw.write(json + LINE_SEPARATOR);
		bw.flush();
	}
}
